//
//  SoftSettingViewController.m
//  Fengshui
//
//  Created by are on 13-5-27.
//
//

#import "SoftSettingViewController.h"
#import "TongxunluViewController.h"
#import "InvitationvViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "SVModalWebViewController.h"

@interface SoftSettingViewController ()

@end

@implementation SoftSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"设置";
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    SinaWeibo *sinaweibo = [self sinaweibo];
    if ([sinaweibo isAuthValid])
    {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"sinaUserName"]) {
            
            return;
        }
        // https://api.weibo.com/2/users/show.json
        NSString *string = [NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json?uid=%@&access_token=%@",sinaweibo.userID,sinaweibo.accessToken];

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:string]];
        [request startSynchronous];
        NSError *error = [request error];
        if (!error) {

            NSDictionary *dic = [[request responseString] JSONValue];
            NSString *nameString = [dic objectForKey:@"screen_name"];
            NSDictionary *sinaDic = [NSDictionary dictionaryWithObject:nameString forKey:@"screen_name"];
            [[NSUserDefaults standardUserDefaults] setObject:sinaDic forKey:@"sinaUserName"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
    
}
- (SinaWeibo *)sinaweibo
{
    AppDelegate  *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.sinaweibo.delegate = self;
    return delegate.sinaweibo;
}
-(void)doLoginButtonAction
{
    LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

      self.menuSection0TitleArr = [NSArray arrayWithObjects:@"八字设置",@"卜卦历史",@"我的收藏",@"手机通讯录好友速配",@"邀请好友",nil];
    self.menuSection1TitleArr = [NSArray arrayWithObjects:@"易学VIP服务",@"易学知识",@"风水知识", nil];
    self.menuSection2TitleArr = [NSArray arrayWithObjects:@"给我们评价",@"给我们反馈",@"关于本应用",@"关于我们", nil];

    UIBarButtonItem *longInBtn = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStyleBordered target:self action:@selector(doLoginButtonAction)];
     
    self.navigationItem.rightBarButtonItem = longInBtn;

    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - tablevieww delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 1) {

        if (indexPath.row == 0) {
            
            SettingViewController *settingViewController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
            [self.navigationController pushViewController:settingViewController animated:YES];
            [settingViewController release];
            
        }
        else if (indexPath.row == 3)
        {
            TongxunluViewController *controller = [[TongxunluViewController alloc] initWithNibName:@"TongxunluViewController" bundle:nil];
            [self.navigationController pushViewController:controller animated:YES];
            [controller release];
        }
        else if (indexPath.row == 4)
        {
            InvitationvViewController *controller = [[InvitationvViewController alloc] initWithNibName:@"InvitationvViewController" bundle:nil];
            [self.navigationController pushViewController:controller animated:YES];
            [controller release];
        }
    }
    else if (indexPath.section == 2)
    {
        switch (indexPath.row) {
            case 2:{
                    NSString*filePath = [[NSBundle mainBundle]pathForResource:@"index"ofType:@"htm"];
                    NSURL*url = [NSURL fileURLWithPath:filePath];
                    SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURL:url];
                    webViewController.modalPresentationStyle = UIModalPresentationPageSheet;
                    webViewController.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsCopyLink | SVWebViewControllerAvailableActionsMailLink;
                    [self presentModalViewController:webViewController animated:YES];
                }
                break;
                
            default:
                break;
        }
    }

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {

        case 0:
            return 2;
            break;
        case 1:
            return [self.menuSection0TitleArr count];
            break;
        case 2:
            return [self.menuSection1TitleArr count];
            break;
        case 3:
            return [self.menuSection2TitleArr count];
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {

        
       // WeiboHaoyouCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseStr];
            
         BangdingCell * cell = [[[NSBundle mainBundle] loadNibNamed:@"BangdingCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {

            NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"userIphoneInfo"];
            if (dic) {

                //@"uphone_num"
                cell.pictureView.image = [UIImage imageNamed:@"tongxunludefault.jpg"];
                cell.nameLabel.text = [NSString stringWithFormat:@"手机号(%@)",[dic objectForKey:@"uphone_num"]];
                [cell.bandingBtn  setTitle:@"已绑定" forState:UIControlStateNormal];
                cell.bandingBtn.tag = 1;
                
            }
            else
            {
                cell.pictureView.image = [UIImage imageNamed:@"tongxunludefault.jpg"];
                cell.nameLabel.text = @"手机号";
                [cell.bandingBtn  setTitle:@"未绑定" forState:UIControlStateNormal];
                cell.bandingBtn.tag = 0;
            }
            cell.typeString = @"phone";
        }
        else if (indexPath.row == 1)
        {
            SinaWeibo *sinaweibo = [self sinaweibo];
            if (![sinaweibo isAuthValid])
            {
                cell.pictureView.image = [UIImage imageNamed:@"tongxunludefault.jpg"];
               [cell.bandingBtn  setTitle:@"未绑定" forState:UIControlStateNormal];
                cell.nameLabel.text = @"新浪微博";
                cell.bandingBtn.tag = 0;
                
                
            }
            else
            {
                NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"sinaUserName"];
                NSString *nameString = [dic objectForKey:@"screen_name"];
                cell.pictureView.image = [UIImage imageNamed:@"tongxunludefault.jpg"];
                [cell.bandingBtn  setTitle:@"已绑定" forState:UIControlStateNormal];
                cell.nameLabel.text = [NSString stringWithFormat:@"新浪微博(%@)",nameString];
                cell.bandingBtn.tag = 1;
            }
            

            cell.typeString = @"sina";
        }
        cell.delegate = self;
        
        
        return cell;
    }
else
{
    NSString *reuseStr = @"moreViewmenutableview";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseStr];
    if( !cell ){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseStr];
    }
    
    switch (indexPath.section) {
        case 1:
            cell.textLabel.text = [self.menuSection0TitleArr objectAtIndex:indexPath.row];
            break;
        case 2:
            cell.textLabel.text = [self.menuSection1TitleArr objectAtIndex:indexPath.row];
            break;
        case 3:
            cell.textLabel.text = [self.menuSection2TitleArr objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }
    return cell;
}
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
            case 0:
            return @"帐号绑定";
            break;
        case 1:
            return @"我的信息";
            break;
        case 2:
            return @"易学资源";
            break;
        case 3:
            return @"关于";
        default:
            return nil;
            break;
    }
}
- (void)storeAuthData
{
    SinaWeibo *sinaweibo = [self sinaweibo];

    NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys:
                              sinaweibo.accessToken, @"AccessTokenKey",
                              sinaweibo.expirationDate, @"ExpirationDateKey",
                              sinaweibo.userID, @"UserIDKey",
                              sinaweibo.refreshToken, @"refresh_token", nil];
    [[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}
- (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo
{
    [self storeAuthData];
    NSString *string = [NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json?uid=%@&access_token=%@",sinaweibo.userID,sinaweibo.accessToken];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:string]];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        
        NSDictionary *dic = [[request responseString] JSONValue];
        NSString *nameString = [dic objectForKey:@"screen_name"];
        NSDictionary *sinaDic = [NSDictionary dictionaryWithObject:nameString forKey:@"screen_name"];
        [[NSUserDefaults standardUserDefaults] setObject:sinaDic forKey:@"sinaUserName"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }
    [self.menuTableView reloadData];
}
#pragma ---
-(void)btnActionOnSina
{
     SinaWeibo *sinaweibo = [self sinaweibo];
     [sinaweibo logIn];
}
-(void)btnActionOnPhone
{
    RegisterViewController *viewController = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
    
}
-(void)btnActionOnSinaJieBang
{
    SinaWeibo *sinaweibo = [self sinaweibo];
    [sinaweibo logOut];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sinaUserName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.menuTableView reloadData];
}
@end
